<template>
  <div class="arcticle-item_container">
    <!-- 一条文章单元格 -->
    <van-cell @click="$router.push('/article?art_id='+item.art_id)">
      <!-- 标题区域的插槽 -->
      <template #title>
        <div class="title-box">
          <!-- 标题 -->
          <span>{{item.title}}</span>
          <!-- 单图 -->
          <img v-if="item.cover.type === 1" class="thumb" :src="item.cover.images[0]" />
        </div>
        <!-- 三张图片 -->
        <div class="thumb-box" v-if="item.cover.type >1">
          <img v-for="(url,index) in item.cover.images" :key="index" class="thumb" :src="url" />
        </div>
      </template>
      <!-- label 区域的插槽 -->
      <template #label>
        <div class="label-box">
          <div>
            <span>{{item.aut_name}}</span>
            <span>{{item.comm_count}}</span>
            <span>{{formatTimeAgoFn(item.pubdate)}}</span>
          </div>
          <!-- 反馈按钮 -->
          <van-icon name="cross" v-if="isshowCross" @click.stop="show1=true" />
          <!-- 一级反馈面板 -->
          <van-action-sheet v-model="show1" :actions="actions1" get-container="body" cancel-text="取消"
            @select="onSelectFn" />
          <!-- 二级反馈面板 -->
          <van-action-sheet v-model="show2" :actions="actions2" get-container="body" cancel-text="返回"
            @select="onSelectFn" />
          <van-dialog v-model="show3" title="反馈其他问题" show-cancel-button get-container="body">
            <van-field v-model.trim="message" type="textarea" autosize label="请输入反馈的内容1-100字" maxlength="100"
              show-word-limit :rules="[{required:true,message:'请输入反馈的内容'}]" />
          </van-dialog>
        </div>
      </template>
    </van-cell>
  </div>
</template>

<script>
import {
  Toast
} from 'vant'
import {
  FIRST_ACTION_SHRRT,
  SECOND_ACTION_SHRRT
} from '@/constant'
import {
  articleDislikeAPI,
  articleReportsAPI
} from '@/api/home'
export default {
  name: 'ArticleItem',
  props: {
    // 父传子的文章对象
    item: {
      type: Object,
      require: true
    },
    // 父传子文章对象在数组中的下标
    i: Number,
    // 控制x的可见性
    isshowCross: {
      type: Boolean,
      default: true
    }
  },
  data () {
    return {
      show1: false,
      show2: false,
      // 一级面板显示的内容
      actions1: FIRST_ACTION_SHRRT,
      // 二级面板显示内容
      actions2: SECOND_ACTION_SHRRT,
      show3: false,
      message: ''
    }
  },
  // created () {
  //   this.item.pubdate = this.formatTimeAgoFn(this.item.pubdate)
  // },
  methods: {
    async onConfirmFn () {
      if (this.msg.length === 0) {
        Toast('请输入反馈内容')
        return
      }
      try {
        await articleReportsAPI({
          artId: this.item.art_id,
          type: '0',
          remark: this.msg
        })
        Toast.success('反馈成功')
      } catch (e) {
        Toast.fail(e.message || '反馈失败')
      }
    },
    async onSelectFn (actionObj) {
      if (actionObj.id === 22) {
        this.show2 = true
      } else if (actionObj.id === 11) { // 不感兴趣
        // 1.删除这篇文章
        // 两次子传父
        this.$emit('del-art', this.i)
        // 2.发请求，告诉后台，从数据库中删除
        try {
          await articleDislikeAPI({
            artId: this.item.art_id
          })
          Toast.success('操作成功')
        } catch (e) {
          Toast.fail(e.message || '操作失败')
        }
      } else if (actionObj.id === 33) { // 拉黑作者
      } else { // 二级反馈-举报
        if (actionObj.id === 0) {
          this.show3 = true
        } else {
          try {
            await articleReportsAPI({
              artId: this.item.art_id,
              type: actionObj.id + '',
              remark: actionObj.name
            })
            Toast.success('反馈成功')
          } catch (e) {
            Toast.fail(e.message || '反馈失败')
          }
        }
        // 隐藏 一级 二级反馈面板
        this.show1 = this.show2 = false
      }
    }
  }
}
</script>

<style scoped lang="less">
  /* 标题样式 */
  .title-box {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
  }

  /* label描述样式 */
  .label-box {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }

  /* 文章信息span */
  .label-box span {
    margin: 0 3px;

    &:first-child {
      margin-left: 0;
    }
  }

  /* 图片的样式, 矩形黄金比例：0.618 */
  .thumb {
    width: 113px;
    height: 70px;
    background-color: #f8f8f8;
    object-fit: cover;
  }

  /* 三图, 图片容器 */
  .thumb-box {
    display: flex;
    justify-content: space-between;
  }

</style>
